package yagwl.client.view.demo;

import yagwl.client.model.ItemDTO;
import yagwl.client.model.demo.ProductListCache;
import yagwl.client.resource.Resources;
import yagwl.client.widget.PagedFlexTable;
import yagwl.client.widget.Section;
import yagwl.client.widget.PagedFlexTable.Action;
import yagwl.client.widget.PagedFlexTable.RowActionCallback;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;

public class ProductListView extends Composite {

  private final Section section = new Section("Products");
  private final PagedFlexTable dataTable;
  private final ProductListCache listCache;
  private final ProductEditForm detailFormPanel;

  public final static String HistoryToken = "productList";

  public ProductListView(ProductListCache listCache,
      ProductEditForm detailFormPanel) {
    dataTable = new PagedFlexTable(listCache);
    this.listCache = listCache;
    this.detailFormPanel = detailFormPanel;
    listCache.setHistoryToken(HistoryToken);
    section.setContent(dataTable);
    drawTable();
    initWidget(section);
  }

  private void drawTable() {
    dataTable.addColumn(Column.Name.ordinal(), "Name");
    dataTable.addColumn(Column.ImageURL.ordinal(), "Image");
    dataTable.addColumn(Column.Description.ordinal(), "Description", "400px");
    dataTable.addColumn(Column.Price.ordinal(), "Price");

    dataTable.setPageSizeOptions(new int[] { 10 });
    dataTable.enableRowCreate(ProductEditForm.HistoryToken,
        "Create New Product");
    dataTable.enableRowDetail(ProductEditForm.HistoryToken, detailFormPanel);
    dataTable.enableRowDelete();
    dataTable.addButton("Manage Product Hieararchy", new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        History.newItem(ProductHierarchy.HistoryToken, true);
      }
    });
    dataTable.setRowActionCallback(new ProductRowActionCallback());

    dataTable.renderUI();
  }

  private class ProductRowActionCallback implements RowActionCallback {

    @Override
    public void action(Action action, int dataRowIdx, int objIdx) {
      ItemDTO prd = (ItemDTO) listCache.getList().get(objIdx);
      switch (action) {
      case Populate:
        dataTable.setCellText(dataRowIdx, Column.Name.ordinal(), prd
            .get("name"));
        if (prd.get("imageUrl") != null && !prd.get("imageUrl").equals(""))
          dataTable.setCellWidget(dataRowIdx, Column.ImageURL.ordinal(),
              new Image(prd.get("imageUrl")));
        else { // clear out previous photo
          // there seems to be a bug, clearCell isnt working always
          // rootTP.clearCell(tableRow, Photo);
          dataTable.setCellWidget(dataRowIdx, Column.ImageURL.ordinal(),
              new Image(Resources.I.trpx()));
        }
        dataTable.setCellText(dataRowIdx, Column.Description.ordinal(), prd
            .get("description"));
        dataTable.setCellText(dataRowIdx, Column.Price.ordinal(), prd
            .get("priceStr"));
        break;
      }
    }
  }

  private enum Column {
    Name, ImageURL, Description, Price
  }

}
